package com.nervenets.web.controller.handler.user.news;

import com.nervenets.web.controller.handler.Handler;
import com.nervenets.web.hibernate.domain.news.News;
import lombok.Data;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

public class NewsListHandler extends Handler<NewsListParams> {
    @Override
    public String handle(NewsListParams params, HttpServletRequest request) throws Exception {
        String hql = "from News where type = ? order by id desc ";
        List<Object> objects = new ArrayList<>();
        objects.add(params.getNewsType());

        List<News> news = userDao.getAllObjectsByHql(hql, objects, params.getStart(), params.getLimit());
        List<Model> models = new ArrayList<>();
        for (News news1 : news) {
            models.add(new Model(news1));
        }
        return successMessage(models);
    }

    @Override
    public String result() throws Exception {
        return successMessage();
    }

    @Data
    class Model {
        private long newsId;
        private String title;
        private int createTime;
        private String source;
        private String smallCoverImage;
        private String bigCoverImage;

        public Model(News news) {
            this.newsId = news.getId();
            this.title = news.getInfo().getTitle();
            this.createTime = news.getCreateTime();
            this.source = news.getInfo().getSource();
            this.smallCoverImage = news.getInfo().getSmallCoverImage();
            this.bigCoverImage = news.getInfo().getBigCoverImage();
        }
    }
}
